package logic

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"zero-demo-rpc/mall/MakeProduct/api/internal/svc"
	"zero-demo-rpc/mall/MakeProduct/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

type ProductList struct {
	id      string `db:"product_id"`
	nftname string `db:"nftname"`
	time    string `db:"post_time"`
	state   int    `db:"product_state"`
	url     string `db:"product_url"`
}

func NewProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductListLogic {
	return &ProductListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductListLogic) ProductList(req *types.ListReq) (resp *types.ListReply, err error) {
	// todo: add your logic here and delete this line

	print("接收商品列表请求：", req.UserName)

	db, err := sql.Open("mysql", "root:huohuo@tcp(127.0.0.1:3306)/test_2?charset=utf8")

	//root为数据库用户名，后面为密码，tcp代表tcp协议，test处填写自己的数据库名称
	rows, err := db.Query("SELECT product_id,product_name,post_time,product_state,product_url FROM product_list where user_name=?", req.UserName)
	var datass = []types.Data{}

	if err != nil {
		return nil, errors.New("")
	} else {

		for rows.Next() {
			var s ProductList
			err := rows.Scan(&s.id, &s.nftname, &s.time, &s.state, &s.url)
			//err := rows.Scan(&s.name, &s.address)
			if err != nil {
				fmt.Println(err)
			}

			data := types.Data{
				Id:      s.id,
				NftName: s.nftname,
				Time:    s.time,
				State:   s.state,
				ImgURL:  "http://192.168.0.50:12302/imgs/" + s.url,
			}
			datass = append(datass, data)
		}
	}

	return &types.ListReply{
		NftList: datass,
	}, err
}
